<?php
namespace Modules\UI\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Setting;
use Validator;
use ThrowException;
use DB;
use Auth;

class SettingController extends Controller
{
    public function show(Request $req, $type)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $setting = [];

        $default = [
            'title' => '未设置'
        ];

        $shareConfig = Setting::get('sys_share') ?: ['a' => 'b'];
        $shareConfig['app_share_url'] = env('APP_URL') . '/shareApp' ;

        if ($type == 'base') {
            $setting = [
                'box_home' => Setting::get('ui_box_home') ?: $default,
                'fudai_home' => Setting::get('ui_fudai_home') ?: $default,
                'shop_home' => Setting::get('ui_shop_home') ?: $default,
                'activity_home' => Setting::get('ui_activity_home') ?: $default,
                'topic_home' => Setting::get('ui_topic_home') ?: $default,
                'login_page' => Setting::get('ui_login_page') ?: $default,
                'fudai_home' => Setting::get('ui_fudai_home') ?: $default,
                'bgm' => Setting::get('sys_bgm') ?: $default,
                // 'rule_page' => Setting::get('ui_rule_page') ?: $default,
                // 'share_rule_page' => Setting::get('ui_share_rule_page') ?: $default,
                'vip_page' => Setting::get('ui_vip_page') ?: $default,
                'open_box' => Setting::get('ui_open_box') ?: $default,
                'box_room' => Setting::get('ui_box_room') ?: $default,
                'score' => Setting::get('sys_score') ?: $default,
                'user_center' => Setting::get('ui_user_center') ?: $default,
                'share' => $shareConfig,
                'order' => Setting::get('sys_order') ?: ['a' => 'b'],
                'box_depot' => Setting::get('sys_box_depot') ?: ['a' => 'b'],
                'coupon_popup' => Setting::get('sys_coupon_popup') ?: ['a' => 'b'],
                'market' => [
                    'is_enabled' => @Setting::get('sys_market')['is_enabled'] ? 1 : 0
                ],
                'exchange' => [
                    'is_enabled' => @Setting::get('sys_exchange')['is_enabled'] ? 1 : 0
                ],
                'custom_service' => Setting::get('sys_custom_service'),
                'is_log_visitor' => env('IS_LOG_VISITOR', true) ? 1 : 0
            ];

            // 淘宝端需要的设置
            if (Auth::platformType() === 'ali') {
                $setting['taobao'] = Setting::get('sys_taobao') ?: ['a' => 'b'];
            }
        }
        else {
            $setting = Setting::get('sys_' . $type) ?: Setting::get('ui_' . $type) ?: $default;
        }

        return [
            'setting' => $setting
        ];
    }

    public function getFudaiRule(Request $req, $id)
    {
        $fd_dmw = DB::select("select * from fudai_activity where id = {$id} ");
        $fd_dmw_res = array_map('get_object_vars', $fd_dmw) ;
        $row_dmw = $fd_dmw_res[0];

        return $row_dmw;
    }
}
